template <typename T>
T* Singleton<T>::instance_ = NULL;

template <typename T>
pthread_mutex_t Singleton<T>::mutex_    = PTHREAD_MUTEX_INITIALIZER;

template <typename T>
T* Singleton<T>::instance()
{
    if (NULL == instance_)
    {
        pthread_mutex_lock(&mutex_);
        if (NULL == instance_)
        {
            instance_ = new T;
        }
        pthread_mutex_unlock(&mutex_);
    }

    return instance_;
}

template <typename T>
void Singleton<T>::cleanup()
{
    if (instance_ != NULL)
    {
        pthread_mutex_lock(&mutex_);
        if (instance_ != NULL)
        {
            delete instance_;
            instance_ = NULL;
        }
        pthread_mutex_unlock(&mutex_);
    }
}
